//
//  OrganationHeaderView.m
//  ShangHaiTuJI
//
//  Created by xiaoxiangtuji on 2017/9/19.
//  Copyright © 2017年 xiaoxiangtuji. All rights reserved.
//

#import "OrganationHeaderView.h"
#import "CatagoryModel.h"



@interface OrganationHeaderView()

@property(nonatomic,strong)OrganationSeekCell *selectedCell;

@end


@implementation OrganationHeaderView

-(instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        [self createCollectionUI];
    }
    return self;
}

/**
 创建collectioview的布局
 */
-(void)createCollectionUI{
    self.collectionView.scrollsToTop = YES;
    self.flowLayout = [[UICollectionViewFlowLayout alloc] init];
    self.flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    self.flowLayout.minimumLineSpacing = scaleLeftAndRight(19);
    self.flowLayout.minimumInteritemSpacing = scaleTopAndBottom(19);
    self.collectionView.showsVerticalScrollIndicator = NO;
    self.collectionView.showsHorizontalScrollIndicator = NO;
    self.flowLayout.itemSize = CGSizeMake(scaleLeftAndRight(52), scaleTopAndBottom(75));
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, self.frame.size.height) collectionViewLayout:self.flowLayout];
    [self addSubview:self.collectionView];
    [self.collectionView registerClass:[OrganationSeekCell class] forCellWithReuseIdentifier:@"OrganationSeekCell"];
    self.collectionView.backgroundColor = [UIColor whiteColor];
    self.collectionView.dataSource = self;
    self.collectionView.delegate = self;
}
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.dataCataArray.count;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    OrganationSeekCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"OrganationSeekCell" forIndexPath:indexPath];
     cell.model = self.dataCataArray[indexPath.row];
    if (_keepActiveIndex == indexPath) {
        cell.transform = CGAffineTransformMakeScale(1.1, 1.1);
        cell.nameLabel.textColor = [UIColor orangeColor];
    }
    return cell;
}
-(UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    return UIEdgeInsetsMake(scaleTopAndBottom(18), scaleLeftAndRight(18), scaleTopAndBottom(18),scaleLeftAndRight(18));
}
//-(void)collectionView:(UICollectionView *)collectionView didEndDisplayingCell:(UICollectionViewCell *)cell forItemAtIndexPath:(NSIndexPath *)indexPath{
//    OrganationSeekCell *cells = (OrganationSeekCell *)cell;
//    if (indexPath == self.selectedIndex) {
//        [UIView animateWithDuration:1 animations:^{
//            cells.transform = CGAffineTransformMakeScale(1.1, 1.1);
//            cells.nameLabel.textColor = [UIColor orangeColor];
//        } completion:^(BOOL finished) {
//
//        }];
//    }
//}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    OrganationSeekCell *cell = (OrganationSeekCell *) [collectionView cellForItemAtIndexPath:indexPath];
    self.selectedIndex = indexPath;
    CatagoryModel *model = self.dataCataArray[indexPath.row];
    self.typeChooseBlock(model.Id,self.selectedIndex);
    [UIView animateWithDuration:0.01 animations:^{
        cell.transform = CGAffineTransformMakeScale(1.1, 1.1);
        cell.nameLabel.textColor = [UIColor orangeColor];
    } completion:^(BOOL finished) {
        
    }];
    
}
-(void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    OrganationSeekCell *cell = (OrganationSeekCell *) [collectionView cellForItemAtIndexPath:indexPath];
    [UIView animateWithDuration:0.01 animations:^{
        cell.transform = CGAffineTransformMakeScale(1.0, 1.0);
        cell.nameLabel.textColor = [UIColor blackColor];
    } completion:^(BOOL finished) {
        
    }];
    
}
-(NSMutableArray *)dataCataArray{
    if (_dataCataArray == nil) {
        _dataCataArray = [[NSMutableArray alloc] init];
        [self getInformationData];
    }
    return _dataCataArray;
}

-(void)getInformationData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    NSString *url = ServerIp@"/institutionApp/categoryListAll";
    [manager POST:url parameters:nil progress:^(NSProgress * _Nonnull uploadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSArray *listArray = jsonDic[@"list"];
        for (NSDictionary *dic in listArray) {
            CatagoryModel *model = [CatagoryModel modelWithDictionary:dic
                                    ];
            [self.dataCataArray addObject:model];
        }
        [self.collectionView reloadData];
        NSLog(@"我要获取中间数据的线程是%@",[NSThread currentThread]);

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
    }];
    
}


@end
